package com.gy.boot.common.util;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 提供对JDK常用类的简单操作工具
 * @author geYang
 * */
public class CommonUtil {

	/** List<String> 转 String */
	public static String listToString(List<String> list, String symbol) {
	    if (BlankUtil.isBlank(list)) {
	        return null;
	    }
		symbol = BlankUtil.isBlank(symbol) ? ";" : symbol;
	    StringBuilder listStr = new StringBuilder();
	    for (String str : list) {
	        listStr.append(symbol);
	        listStr.append(str);
	    }
        return listStr.substring(1);
	}

	/** 获取数组字段下标 */
	public static Map<String, Integer> getKeyIndex(String[] arr) {
		Map<String, Integer> keyIndex = MapUtil.newHashMap(2, true);
		if (arr == null || arr.length == 0) {
			return keyIndex;
		}
		for (int i = 0; i < arr.length; i++) {
			keyIndex.put(arr[i], i);
		}
		return keyIndex;
	}

	/**
	 * 从List中通过index获取元素,避免越界异常
	 * */
	public static String getValue(List<Object> list, Integer index) {
		if (index == null || list == null || list.isEmpty() || list.size() <= index) {
			return null;
		}
		Object obj = list.get(index);
		String value = StrTool.valueOfStr(obj);
		if (BlankUtil.isBlankNull(value)) {
			return null;
		}
		if (obj instanceof Double) {
			if (value.contains("E")) {
				return NumberUtil.toBigDecimal(value).toString();
			}
			return value;
		}
		String errStr = "\"";
		if (value.startsWith(errStr)) {
			value = StrUtil.removeAll(value, errStr);
		}
		if (BlankUtil.isBlankNull(value)) {
			return null;
		}
		return value.trim();
	}



}
